package com.lepu.app.fun.index.remind;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.app.utils.DBHelper;
import com.core.lib.utils.main.UtilityBase;
import com.lepu.app.fun.chat.util.DbQueryRunner;
import com.lepu.app.fun.my.bean.BeanMsgBbs;

public class AlarmTipsManager {
	 private static final String TABLE_NAME = "alarm";
    /**
     * 插入定时器 列表
     *
     * @param
     */
    public static boolean insertAlarmList(List<Alarm> alarmList) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;
        if(null != alarmList && 0 < alarmList.size()){
        	for(int i = 0; i < alarmList.size(); i++){
        		Alarm item = alarmList.get(i);
        		ContentValues values = new ContentValues();
                values.put("type", item.type);
                values.put("alarmId", item.alarmId);
                values.put("alarmTime", item.alarmTime);
                values.put("time", item.time);
                values.put("week", item.week);
                values.put("yaoId", item.yaoId);
                values.put("yaoName", item.yaoName);
                values.put("yaoType", item.yaoType);
                values.put("setTime", item.setTime);
                flag = dbHelp.insert(TABLE_NAME, null, values);
        	}
        }

        return flag;
    }

    /**
     * 插入定时器
     *
     * @param
     */
    public static boolean insertAlarm(Alarm item) {
    	 boolean flag = false;

         DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
         if (dbHelp == null)
             return flag;
         
		ContentValues values = new ContentValues();
        values.put("type", item.type);
        values.put("alarmId", item.alarmId);
        values.put("alarmTime", item.alarmTime);
        values.put("time", item.time);
        values.put("week", item.week);
        values.put("yaoId", item.yaoId);
        values.put("yaoName", item.yaoName);
        values.put("yaoType", item.yaoType);
        values.put("setTime", item.setTime);
        flag = dbHelp.insert(TABLE_NAME, null, values);
    	
        return flag;
        
    }


    //获取所有数据
    public static ArrayList<Alarm> getAlarmByName(String name) {
    	ArrayList<Alarm> provinces = new ArrayList<Alarm>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
		if (dbHelp == null)
			return provinces;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] { 
					"id","type", "alarmId", "alarmTime", "time", "week", 
					"yaoId","yaoName","yaoType","setTime"}, 
					"yaoName=?",new String[] {name}, null, null,null);

			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		return provinces;
    }
    
    //获取闹钟药品分类
    public static ArrayList<Alarm> getAlarmByGroupName() {
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
		if (dbHelp == null)
			return null;

		String sql = "SELECT * from alarm  GROUP BY yaoName order by id desc";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,null);

		if (queryResult == null || queryResult.size() == 0) {
			return null;
		}

		ArrayList<Alarm> dataList = new ArrayList<Alarm>();
		for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
			HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
			Alarm item = parseFromDatabase(dictionary);
			dataList.add(item);
		}
		return dataList;
	}
    
    //删除某一条记录
    public static boolean deleteItem(int alarmId) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "alarmId=?",new String[]{String.valueOf(alarmId)});

        return flag;
    }
    
    public static boolean deleteByName(String name) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "yaoName=?",new String[]{String.valueOf(name)});

        return flag;
    }
    
    /**
     * 更新定时器
     *
     * @param
     */
    public void updateAlarm(Alarm alarm) {
        DbQueryRunner queryRunner = DbQueryRunner.getInstance();

        try {
            String sql = "update alarm set alarmTime=? where id=?";
            Object params[] = {alarm.alarmTime, alarm.id};
            queryRunner.update(sql, params);
        } catch (Exception e) {
            android.util.Log.d(e.toString(), e.getMessage() == null ? "" : e.getMessage());
        }
    }

    /**
     * 获取最近的闹钟
     * @return
     */
    public List<Alarm> getAlarmCurrent() {

        List<Alarm> alarms = null;
        try {
            String sql = "SELECT * from alarm  ORDER BY alarmTime desc";
            //todo 进行了修改
            //String[] params = new String[]{userid};
            DbQueryRunner queryRunner = DbQueryRunner.getInstance();
            alarms = queryRunner.query(sql, new DbQueryRunner.RowHandler() {
                @Override
                public Object handle(Cursor cursor) {
                    Alarm alarm = new Alarm();
                    alarm.id = cursor.getInt(cursor.getColumnIndex("id"));
                    alarm.type = cursor.getString(cursor.getColumnIndex("type"));
                    alarm.alarmId = cursor.getInt(cursor.getColumnIndex("alarmId"));
                    alarm.alarmTime = cursor.getLong(cursor.getColumnIndex("alarmTime"));
                    alarm.setTime = cursor.getLong(cursor.getColumnIndex("setTime"));
                    alarm.time = cursor.getString(cursor.getColumnIndex("time"));
                    alarm.week = cursor.getString(cursor.getColumnIndex("week"));
                    alarm.yaoId = cursor.getInt(cursor.getColumnIndex("yaoId"));
                    alarm.yaoName = cursor.getString(cursor.getColumnIndex("yaoName"));
                    alarm.yaoType = cursor.getString(cursor.getColumnIndex("yaoType"));
                    alarm.userid = cursor.getString(cursor.getColumnIndex("userid"));
                    return alarm;
                }
            });
        } catch (Exception e) {
        }

        return alarms;
    }


    /**
     * 查看的是否有同名的提醒文件
     *
     * @param medicationName
     * @param
     */
    public boolean isExist(String medicationName) {
      	ArrayList<Alarm> provinces = new ArrayList<Alarm>();
    		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
    		if (dbHelp == null)
    			return false;

    		SQLiteDatabase dbSqlite = null;
    		try {
    			dbSqlite = dbHelp.getWritableDatabase();

    			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] { 
    					"id","type", "alarmId", "alarmTime", "time", "week", 
    					"yaoId","yaoName","yaoType","setTime"}, 
    					"yaoName=?",new String[] {medicationName}, null, null,null);

    			cursorParse(provinces, queryResult);
    			dbSqlite.close();
    		} catch (Exception e) {
    		}
    		
			 if (provinces.size() > 0) {
		            return true;
		        } else {
		            return false;
		      }
        
    }

    static class AlarmManagerHolder {
        static AlarmTipsManager alarmManager = new AlarmTipsManager();
    }

    public static AlarmTipsManager getInstance() {
        return AlarmManagerHolder.alarmManager;
    }
    
	/**
	 * 解析cursor转换为对象
	 */
	private static void cursorParse(ArrayList<Alarm> msgArray,Cursor cursor) {
		while (cursor.moveToNext()) {
			Alarm beanAlarm = new Alarm();
			beanAlarm.id = (cursor.getInt(cursor.getColumnIndex("id")));
			beanAlarm.type = (cursor.getString(cursor.getColumnIndex("type")));
			beanAlarm.alarmId = (cursor.getInt(cursor.getColumnIndex("alarmId")));
			beanAlarm.alarmTime = (cursor.getLong(cursor.getColumnIndex("alarmTime")));
			beanAlarm.time = (cursor.getString(cursor.getColumnIndex("time")));
			beanAlarm.week = (cursor.getString(cursor.getColumnIndex("week")));
			beanAlarm.yaoId = (cursor.getInt(cursor.getColumnIndex("yaoId")));
			beanAlarm.yaoName = (cursor.getString(cursor.getColumnIndex("yaoName")));
			beanAlarm.yaoType = (cursor.getString(cursor.getColumnIndex("yaoType")));
			beanAlarm.setTime = (cursor.getLong(cursor.getColumnIndex("setTime")));
			msgArray.add(beanAlarm);
		}
	}
	
	private static Alarm parseFromDatabase(HashMap<String, Object> dictionary) {

		if (dictionary == null || dictionary.size() == 0) {
			return null;
		}
		Alarm item = new Alarm();

		item.id = UtilityBase.parseInt((String) dictionary.get("id"));
		item.type = UtilityBase.parseString((String) dictionary.get("type"));
		item.alarmId = UtilityBase.parseInt((String) dictionary.get("alarmId"));
		item.alarmTime = UtilityBase.parseLong((String) dictionary.get("alarmTime"));
		item.time = UtilityBase.parseString((String) dictionary.get("time"));
		item.week = UtilityBase.parseString((String) dictionary.get("week"));
		item.yaoId = UtilityBase.parseInt((String) dictionary.get("yaoId"));
		item.yaoName = UtilityBase.parseString((String) dictionary.get("yaoName"));
		item.yaoType = UtilityBase.parseString((String) dictionary.get("yaoType"));
		item.setTime = UtilityBase.parseLong((String) dictionary.get("setTime"));

		return item;
	}
}
